package cn.com.lyj6851.message.redis;

import cn.com.lyj6851.message.listener.RedisKeyExpirationListener;
import cn.com.lyj6851.modules.activity.service.YdyStorePinkService;
import cn.com.lyj6851.modules.order.service.YdyStoreOrderService;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * redis监听配置
 */

@Configuration
@AllArgsConstructor
public class RedisListenerConfig {

	private final RedisTemplate<String, String> redisTemplate;
	private final RedisConfigProperties redisConfigProperties;
	private final YdyStoreOrderService storeOrderService;
	private final YdyStorePinkService storePinkService;

	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory factory) {
		String topic = StrUtil.format("__keyevent@{}__:expired", redisConfigProperties.getDatabase());
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(factory);
		container.addMessageListener(new RedisKeyExpirationListener(redisTemplate,redisConfigProperties
		 ,storeOrderService,storePinkService), new PatternTopic(topic));
		return container;
	}

}

